﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bind.View
{
    /// <summary>
    /// BindView.xaml 的交互逻辑
    /// </summary>
    public partial class BindView : UserControl
    {
        ViewModel.BindViewModel viewModel = new ViewModel.BindViewModel();
        public static DAL.BindDAL bindDAL = null;
        List<BaseModel.MesDb.HkcProgramBind> hkcProgramBinds = null;
        public BindView()
        {
            try
            {
                InitializeComponent();
                bindDAL = new DAL.BindDAL();
                DataContext = viewModel;
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }

        }
        private void List_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MainWindow.viewModel.User != "余佳彬" && MainWindow.viewModel.User != "康锐" && MainWindow.viewModel.User != "戴卫")
                {
                    HandyControl.Controls.MessageBox.Warning("当前账号无权限");
                    return;
                }
                string type = TextBoxType.Text.Trim();
                string ldrom = TextBoxLdrom.Text.Trim();
                string aprom = TextBoxAprom.Text.Trim();
                string mcu = TextBoxMcu.Text.Trim();
                var mesModel = hkcProgramBinds.FirstOrDefault(it => it.type == type);
                if (mesModel !=null)
                {
                    MessageBoxResult result = HandyControl.Controls.MessageBox.Show(
                "是否对同款机型进行更改？", "发现有同款机型", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        mesModel.type = type;
                        mesModel.ldrom = ldrom;
                        mesModel.aprom = aprom;
                        mesModel.mcu = mcu;
                        mesModel.user = MainWindow.viewModel.User;
                        mesModel.time = bindDAL.db.GetDate().ToString("G");
                        int count = bindDAL.db.Updateable(mesModel).ExecuteCommand();
                        if (count != 1)
                        {
                            HandyControl.Controls.Growl.Warning("上传失败");
                            return;
                        }
                    }
                }
                else
                {
                    var model = new BaseModel.MesDb.HkcProgramBind()
                    {
                        type = type,
                        ldrom = ldrom,
                        aprom = aprom,
                        mcu = mcu,
                        user = MainWindow.viewModel.User,
                        time = bindDAL.db.GetDate().ToString("G")
                    };
                    int count = bindDAL.db.Insertable(model).ExecuteCommand();
                    if (count != 1)
                    {
                        HandyControl.Controls.Growl.Warning("上传失败");
                        return;
                    }
                }
                hkcProgramBinds = bindDAL.GetBinds();
                ObservableCollection<ViewModel.BindViewModel.BindItem> items = new ObservableCollection<ViewModel.BindViewModel.BindItem>();
                foreach (var item in hkcProgramBinds)
                {
                    ViewModel.BindViewModel.BindItem bindItem = new ViewModel.BindViewModel.BindItem
                    {
                        Type = item.type,
                        Mcu = item.mcu,
                        Ldrom = item.ldrom,
                        Aprom = item.aprom,
                        User = item.user,
                        Time = item.time
                    };
                    items.Add(bindItem);
                }
                viewModel.BindItems = items;
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }
        }

        private void ListItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (BindingList.SelectedIndex != -1)
            {
                var selectItem = viewModel.BindItems[BindingList.SelectedIndex];
                TextBoxType.Text = selectItem.Type;
                TextBoxMcu.Text = selectItem.Mcu;
                TextBoxLdrom.Text = selectItem.Ldrom;
                TextBoxAprom.Text = selectItem.Aprom;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                hkcProgramBinds = bindDAL.GetBinds();
                ObservableCollection<ViewModel.BindViewModel.BindItem> items = new ObservableCollection<ViewModel.BindViewModel.BindItem>();
                foreach (var item in hkcProgramBinds)
                {
                    ViewModel.BindViewModel.BindItem bindItem = new ViewModel.BindViewModel.BindItem
                    {
                        Type = item.type,
                        Mcu = item.mcu,
                        Ldrom = item.ldrom,
                        Aprom = item.aprom,
                        User = item.user,
                        Time = item.time
                    };
                    items.Add(bindItem);
                }
                viewModel.BindItems = items;
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }
        }
    }
}
